﻿$(function () {
    var Page_Size = 10;

    $("#btnFilterLog").live("click", GetDataClick);
    $("#btnSelectAll").live("click", SelectAll);//全部选中
    $("#btnCancelAll").live("click", CancelAll);//全部取消
    $(".checkFlog").live("click", CheckFlogClick);//勾选框点击事件
    $("#Sdate").live("focus", txtSdateConfig);//开始日期控件配置
    $("#Edate").live("focus", txtEdateConfig);//截止日期控件配置
    $("#btnGoNextStep").live("click", btnGoNextStepClick);//送下一步点击事件
    $(".tdViewSelfInfo").live("click", tdViewSelfInfoClick);//个人信息查看
    $(".tdViewCheck").live("click", tdViewCheckClick);//资料审核
    
    Init();

    function Init() {
        GetDataClick();
    }

    //送下一步流程 其实只是表示当前步骤为已完成并且发送站内消息
    function btnGoNextStepClick() {
        if (flowStepCheck()) {
            var sDate = $("#Sdate").val();
            var eDate = $("#Edate").val();
            var message = $("#textMessage").val();
            var checkedList = $(".checkFlog:checked");
            var FlowLogIds = "";
            $.each(checkedList, function (key, item) {
                var $this = $(item);
                var id = $this.attr("data-flowLogId");
                FlowLogIds += id + "#";
            });
            if (FlowLogIds == "") {
                alert("尚未勾选任何数据");
            } else {
                TransferLogToNextStep(FlowLogIds, sDate, eDate, message);
            }
        } else {
            alert("请勾选处于同一步骤下的数据");
        }
    };

    function TransferLogToNextStep(ids, sDate, eDate, message) {
        var url = "RegistHandler/TransferLogToNextStep";
        $.ajax({
            url: url,
            data: { ids: ids, sDate: sDate, eDate: eDate, message: message },
            dataType: "json",
            beforeSend: function () {

            },
            type: "post",
            success: function (res) {
                if (res.Result.IsSuccess) {
                    alert("处理成功");
                    GetDataClick();
                }
                else {
                    alert(res.Result.ErrMessage);
                }
            },
            async: true,
            complete: function () {
            }
        });
    };

    function txtSdateConfig() {
        var endValue = new Date();
        WdatePicker({
            minDate: '1900-01-01', maxDate: endValue,
            onpicked: function () {

            }
        });
    }

    function txtEdateConfig() {
        var endValue = new Date();
        WdatePicker({
            minDate: '1900-01-01',
            onpicked: function () {

            }
        })
    }

    function SelectAll() {
        $(".checkFlog").attr("checked", "checked");
        if (!flowStepCheck()) {
            alert("请勾选处于同一步骤下的数据");
            $(".checkFlog").removeAttr("checked");
        }
    }

    function CancelAll() {
        $(".checkFlog").removeAttr("checked");
    }

    //勾选框点击事件
    function CheckFlogClick() {
        var $this = $(this);
        $($this.attr("checked") != undefined && $this.attr("checked"))
        {
            if (!flowStepCheck()) {
                alert("请勾选处于同一步骤下的数据");
            }
        }
    }
    //勾选框勾选限制，只有同一步骤的才能一块选择
    function flowStepCheck() {
        var flowStepName = "";
        var res = true;
        var checkedList = $(".checkFlog:checked");

        for (var i = 0; i < checkedList.length; i++) {
            var $this = $(checkedList[i]);
            var stepName = $this.attr("data-flowStepName");
            if (i == 0) {
                flowStepName = stepName;
            } else {
                if (flowStepName != stepName) {
                    res = false;
                    break;
                }
            }
        };

        return res;
    };

    function FlowLogFilter(flowStepId, page) {
        if (flowStepId == "-1") flowStepId = "";
        this.FlowStepId = flowStepId;
        this.PageSize = Page_Size;
        this.Page = page;
    }

    function GetDataClick() {
        var flowStepId = $("#flowStepList option:selected").val();
        var filter = new FlowLogFilter(flowStepId, 1);
        GetflowLogPage(filter);
    }

    function GetflowLogPage(f) {
        //分页控件   
        var count = GetDataCount(f);
        $("#pp").pagination(count, {
            link_to: "javascript:void(0)",
            num_edge_entries: 2, //边缘页数
            num_display_entries: 7, //主体页数
            callback: onSelectPageFun,
            items_per_page: Page_Size, //每页显示项
            prev_text: "&laquo;",
            next_text: "&raquo;"
        });

        //响应事件
        function onSelectPageFun(page) {
            f.Page = page + 1;
            var url = "RegistHandler/GetFlowLogs";
            $.ajax({
                url: url,
                data: f,
                dataType: "json",
                beforeSend: function () {

                },
                type: "post",
                success: function (result) {
                    renderFun(result);//响应事件
                },
                async: true,
                complete: function () {
                }
            });
        }
        //END
    };

    Handlebars.registerHelper('logHideInfo', function (log) {
        var res = "";
        for (var p in log) {
            res += "'"+p+"':'" + log[p] + "',";
        }
        res = "{" + res.substring(0,(res.length-1)) + "}";
        return res;
    });

    //数据渲染
    function renderFun(result) {
        if (result.Result.IsSuccess) {
            var $tbody = $("#tbFlowLog tbody");
            $tbody.empty();
            var list = result.FlowLogs;
            var source = $("#FlowLog-template").html();
            var template = Handlebars.compile(source);
            var html = template({ logs: list });
            $tbody.append(html);
        }
        else {
            alert(result.Result.ErrMessage)
        }
    }
    function GetDataCount(f) {
        var count = 0;
        $.post("RegistHandler/GetFlowLogsCount", f, function (res) {
            count = res;
        });
        return count;
    };

    function tdViewSelfInfoClick() {
        var str = $(this).attr("data-DialogInfo");
        var o = eval("(" + str + ")");
        var source = $("#logUserInfo-template").html();
        var template = Handlebars.compile(source);
        var html = template(o);
        myDialog = art.dialog({
            content: html,
            title: '个人信息',
            id: 'logInfoDialog',
        }).lock();
    };

    function tdViewCheckClick()
    {
        var logId = $(this).attr("data-logid");
        var url = "RegistHandler/InfoCheck?flowLogId=" + logId;
        window.open(url, "_self");
    }

});